<?php
/* $Id: funcs.php,v 1.1 JOP $ */
/**
 * @package Issue-Tracker
 * @subpackage Units
 */
if (strpos($_SERVER['PHP_SELF'],basename(__FILE__)) !== false) 
{print "Direct module access forbidden.";exit;}


/* {{{ Function: compute_units */
/**
 * Compute NB units for a user / duration
 * @return string
 */
function compute_units($userid,$duration)
{

 if ($duration <= 3) {
  return user_unit($userid,1) * $duration;
  } else {
  return user_unit($userid,2) * ($duration / 4);
  }

}
/* }}} */

function delete_unit_record($recid)
{

  global $dbi;

  $sql  = "DELETE FROM z_units ";
  $sql .= "WHERE recid='".$recid."'";
  $dbi->query($sql);

}
/* }}} */


/* {{{ Function: user_unit */
/**
 * Return NB units for a user
 * @return string
 */
function user_unit($userid,$basis=null)
{
  $user_pid = getfield('users','pid','userid',$userid);
  $user_gid = getfield('group_users','gid','userid',$userid);

  global $dbi;

  $sql  = "SELECT units ";
  $sql .= "FROM z_units ";
  $sql .= "WHERE gid='$user_gid' ";
  $sql .= "AND profileid='$user_pid' ";
  $sql .= "AND basis='$basis'";
  $result = $dbi->query($sql);
  if ($dbi->num_rows($result) > 0) {
    list($units) = $dbi->fetch($result);
    return $units;
  }

  push_error ("No Units retrieved for this user.");
  return "0";

}
/* }}} */

?>